package com.upay.android.client.handler;

import static com.upay.android.client.helper.Keys.TOKEN;
import static com.upay.android.client.helper.Keys.UPAY_PREFERENCES;
import static com.upay.android.client.helper.Keys.USERID;
import android.content.Intent;
import android.content.SharedPreferences;

import com.upay.android.client.activity.AbstractUPayActivity;
import com.upay.android.client.activity.GroupSubscribedActivity;
import com.upay.domain.authentication.AuthenticationToken;
import com.upay.domain.response.ResponseEnvelope;

public class SignInCallbackHandler extends AbstractCallbackHandler<AuthenticationToken> {
	
	/* ---- Constants ---- */
	
	/* ---- Instance Variables ---- */
	
	/* ---- Constructors ---- */

	public SignInCallbackHandler(AbstractUPayActivity context) {
		this.context = context;
	}
	
	/* ---- Business Methods ---- */
	

	@Override
	public void handleResponse(ResponseEnvelope<AuthenticationToken> response) {
		AuthenticationToken authenticationToken = response.getResponseBody().getResponseObject();
		
		// Save Credentials
        SharedPreferences sharedPreferences = context.getSharedPreferences(UPAY_PREFERENCES, 0);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(USERID, authenticationToken.getUserid());
        editor.putString(TOKEN, authenticationToken.getToken());
        editor.commit();
                
        Intent intent = new Intent(context, GroupSubscribedActivity.class);
        context.startActivity(intent);
	}

	@Override
	public void handleValidationException(ResponseEnvelope<AuthenticationToken> response) {
		context.showValidationErrors("Incorrect account details, please try again");

	}

}
